import { 战利品, ItemData, 手枪, SeasonData } from "../Datas/Data";
import { Constant, ItemType } from "../Framework/Const/Constant";
import { DataManager } from "../Framework/Managers/DataManager";
import { PoolManager } from "../Framework/Managers/PoolManager";
import PrefsManager from "../Framework/Managers/PrefsManager";
import { UIManager } from "../Framework/Managers/UIManager";
import NodeUtil from "../Framework/Utils/NodeUtil";
import { ResourceUtil } from "../Framework/Utils/ResourceUtil";
import GoodsItem from "./GoodsItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SeasonItem extends cc.Component {

    TitleLabel: cc.Label = null;
    Content_0: cc.Node = null;
    Content_1: cc.Node = null;
    TitleBG: cc.Node = null;
    Content: cc.Node = null;

    data: SeasonData;

    goodsItem_1: GoodsItem = null;
    goodsItem_2: GoodsItem = null;

    index: number = 0;

    protected onLoad(): void {
        this.TitleLabel = NodeUtil.GetComponent("TitleLabel", this.node, cc.Label);
        this.Content_0 = NodeUtil.GetNode("Content_0", this.node);
        this.Content_1 = NodeUtil.GetNode("Content_1", this.node);
        this.TitleBG = NodeUtil.GetNode("TitleBG", this.node);
        this.Content = NodeUtil.GetNode("Content", this.node);
    }

    Init(index: number, data: SeasonData) {
        this.index = index;
        this.data = data;
        this.TitleLabel.string = `${index}`;

        if (this.goodsItem_1) PoolManager.PutNode(this.goodsItem_1.node);
        if (this.goodsItem_2) PoolManager.PutNode(this.goodsItem_2.node);

        PoolManager.GetNode("Prefabs/UI/GoodsItem", this.Content_0).then((node) => {
            let goods = node.getComponent(GoodsItem);
            goods.Init(this.data.ItemData_1, this.GoodsItemCallback.bind(this));
            goods.Get.active = DataManager.GetSeasonItemGot(this.index, 1);
            goods.Locked.active = DataManager.SeasonLv < this.index;
            this.goodsItem_1 = goods;
        });
        PoolManager.GetNode("Prefabs/UI/GoodsItem", this.Content_1).then((node) => {
            let goods = node.getComponent(GoodsItem);
            goods.Init(this.data.ItemData_2, this.GoodsItemCallback.bind(this));
            goods.Get.active = DataManager.GetSeasonItemGot(this.index, 2);
            if (PrefsManager.GetBool(Constant.Key.UnlockSeason)) {
                goods.Locked.active = DataManager.SeasonLv < this.index;
            } else {
                goods.Locked.active = true;
            }
            this.goodsItem_2 = goods;
        });

        this.TitleBG.color = DataManager.SeasonLv < this.index ? cc.Color.BLACK : cc.Color.WHITE;
        this.Content.color = DataManager.SeasonLv < this.index ? cc.Color.BLACK : cc.Color.WHITE;
    }

    GoodsItemCallback(data: ItemData) {
        let goodsIndex = 1;
        if (data == this.data.ItemData_1) goodsIndex = 1;
        if (data == this.data.ItemData_2) goodsIndex = 2;
        DataManager.SetSeasonItemGot(this.index, goodsIndex)
        UIManager.Instance.ShowPanel(Constant.Panel.RewardPanel, [data])
        this.Refresh();

        if (data.Type == ItemType[ItemType.金钱]) {
            DataManager.Money += data.Count;
        } else {
            DataManager.AddPlayerItem(data);
        }
    }

    Refresh() {
        if (this.goodsItem_1) {
            this.goodsItem_1.Get.active = DataManager.GetSeasonItemGot(this.index, 1);
            this.goodsItem_1.Locked.active = DataManager.SeasonLv < this.index;
        };

        if (this.goodsItem_2) {
            this.goodsItem_2.Get.active = DataManager.GetSeasonItemGot(this.index, 2);
            if (PrefsManager.GetBool(Constant.Key.UnlockSeason)) {
                this.goodsItem_2.Locked.active = DataManager.SeasonLv < this.index;
            } else {
                this.goodsItem_2.Locked.active = true;
            }
        };

        this.TitleBG.color = DataManager.SeasonLv < this.index ? cc.Color.BLACK : cc.Color.WHITE;
        this.Content.color = DataManager.SeasonLv < this.index ? cc.Color.BLACK : cc.Color.WHITE;
    }
}
